package com.app.micaihu.ad.detail;

import com.app.micaihu.ad.AdConfig;
import com.app.micaihu.ad.toutiao.TTAdManagerHolder;
import com.app.micaihu.bean.ad.NativeAd;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.Utils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.HashMap;
import java.util.List;

public class ADRequest {

    public void getAdView(NativeAd nativeAd, String key) {
        if (nativeAd == null) return;
        String mPosId = nativeAd.getAdId();
        String adType = nativeAd.getAdType();
        if (AdConfig.AD_TYPE_TOUTIAO.equals(adType)) {
            TTAdManager ttAdManager = TTAdManagerHolder.get();
            TTAdNative mTTAdNative = ttAdManager.createAdNative(Utils.getApp().getApplicationContext());
//            HashMap<String, String> map = new HashMap<>();
//            map.put(AdMapUtils.adPositionList + AdMapUtils.adRequestKey, mPosId);
//            MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
            mTTAdNative.loadNativeExpressAd(getAdSlot(mPosId), new TTAdNative.NativeExpressAdListener() {
                @Override
                public void onError(int code, String message) {
//                    HashMap<String, String> map = new HashMap<>();
//                    map.put(AdMapUtils.adPositionList + "onError", mPosId);
//                    map.put(AdMapUtils.adPositionList + "errorMsg", message);
//                    map.put(AdMapUtils.adPositionList + "errorCode", code + "");
//                    MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
                }

                @Override
                public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                    LogUtils.e("List<TTNativeExpressAd>", list.size());
//                    HashMap<String, String> map = new HashMap<>();
//                    map.put(AdMapUtils.adPositionList + AdMapUtils.adSuccessKey, mPosId);
//                    MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
                    if (list != null) {
                        int size = list.size();
                        if (size > 0) {
                            TTNativeExpressAd ttNativeExpressAd = list.get(0);
                            if (adListener != null) {
                                if (ttNativeExpressAd != null) {
                                    ttNativeExpressAd.render();
                                    adListener.adLoad(ttNativeExpressAd, key);
                                }
                            }
                        }
                    }
                }
            });
        }

    }

    /**
     * 返回用户请求的信息
     *
     * @return
     */
    private AdSlot getAdSlot(String mPosId) {
        //feed广告请求类型参数
        return new AdSlot.Builder()
                .setCodeId(mPosId)
                .setSupportDeepLink(true)
                .setExpressViewAcceptedSize(ScreenUtils.getAppScreenWidth(), 0)
                .setAdCount(1)//最多支持3个广告
                .build();
    }

    private AdListener adListener;

    public void setAdListener(AdListener adListener) {
        this.adListener = adListener;
    }
}
